package com.ruoyi.service.impl;

import com.ruoyi.domain.AdvBroadcast;
import com.ruoyi.domain.AdvPlatinfos;
import com.ruoyi.domain.AdvProducts;
import com.ruoyi.mapper.AdvBroadcastMapper;
import com.ruoyi.mapper.AdvPlatinfosMapper;
import com.ruoyi.mapper.AdvProductsMapper;
import com.ruoyi.service.UtilsService;
import com.ruoyi.utils.MyFileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;

@Slf4j
@Service
public class UtilsServiceImpl implements UtilsService {
    private static Boolean isRunning = false;
    @Value("${ruoyi.profile}")
    private String fileStartPath;
    @Autowired
    private AdvPlatinfosMapper advPlatinfosMapper;
    @Autowired
    private AdvProductsMapper advProductsMapper;
    @Autowired
    private AdvBroadcastMapper advBroadcastMapper;

    @Override
    public String compareResourcesDelete() {
        // 放到一个单独的线程中执行
        if(!isRunning) {
            isRunning = true;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Map<String, String> fileMap = new HashMap<String, String>();
                    //首先需要把目标路径中的所有文件取出来
                    File rootFile = new File(fileStartPath);
                    fileMap = directoryHandle(fileMap, rootFile);
                    System.out.println("所有文件" + fileMap);
                    // 把带有文件的所有内容查询出来
                    List<AdvPlatinfos> advPlatinfos = advPlatinfosMapper.selectAdvPlatinfosList(new AdvPlatinfos());
                    List<AdvProducts> advProducts = advProductsMapper.selectAdvProductsList(new AdvProducts());
                    List<AdvBroadcast> broadcasts = advBroadcastMapper.selectAdvBroadcastList(new AdvBroadcast());
                    for (AdvProducts products : advProducts) {
                        String productImage = products.getProductImage();
                        String detailImage = products.getDetailImage();
                        String sceneImage1 = products.getSceneImage1();
                        String sceneImage2 = products.getSceneImage2();
                        String productUrl = products.getProductUrl();
                        clear(productImage, fileMap);
                        clear(detailImage, fileMap);
                        clear(sceneImage1, fileMap);
                        clear(sceneImage2, fileMap);
                        clear(productUrl, fileMap);
                    }
                    for (AdvPlatinfos platinfos : advPlatinfos) {
                        String videourl = platinfos.getVideourl();
                        String photoimage = platinfos.getPhotoimage();
                        clear(videourl, fileMap);
                        clear(photoimage, fileMap);
                    }
                    for (AdvBroadcast broadcast : broadcasts) {
                        String image = broadcast.getImage();
                        clear(image, fileMap);
                    }
                    System.out.println("处理后文件" + fileMap);
                    // 剩下的文件都是没有被使用的文件
                    Collection<String> values = fileMap.values();
                    for (String filePath :values) {
                        File needClearFile = new File(filePath);
                        needClearFile.delete();
                    }
                    isRunning = false;
                }
            }).start();
            return "开始处理...";
        }else {
            return "正在处理, 请稍等...";
        }
    }
    private void clear(String url, Map<String, String> fileMap) {
        String res = fileMap.get(url);
        if(!Objects.isNull(res)) {
            fileMap.remove(url);
        }
    }
    private Map<String, String> directoryHandle(Map<String, String> fileMap, File directory) {
        if(directory.isDirectory()) {
            File[] files = directory.listFiles();
            for (File file : files) {
                directoryHandle(fileMap, file);
            }
        } else {
            fileMap.put("/profile" + (directory.getAbsolutePath().replace("\\", "/").replace(fileStartPath, "")), directory.getAbsolutePath());
        }
        return fileMap;
    }
}
